﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Entidades;

namespace Datos
{
   public class MySqlControlSolicitudDAO:IControlSolicitud
    {
        MySqlConnection conector = new MySqlConnection();
        string connectionString;
        public string s;

        public void iniciarConexion()
        {
            try
            {
                connectionString = "Server=127.0.0.1;Database=justime;Uid=root; pwd=root;";
                conector.ConnectionString = connectionString;
                conector.Open();
                s = "La conexion se ha realizado con exito";

            }
            catch
            {
                s = "Ocurrio un error al intentar conectarse";
            }

        }
        public void Conectar()
        {
            iniciarConexion();
        }

        public void Desconectar()
        {
            conector.Close();
        }

        public string getSolicitud(string placa)
        {
            string email = null;
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "Select * From controlsolicitud where placa=?placa";
            cmd.Parameters.AddWithValue("?placa", placa);
            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read()) {
                email = reader["email"].ToString();
            }

            return email;

        }


        public void insertar(ControlSolicitud con)
        {
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "insert into controlsolicitud(placa,email) Values" +
                "(?placa,?email)";
            cmd.Parameters.AddWithValue("?placa", con.taxi.Placa);
            cmd.Parameters.AddWithValue("?email",con.pasajero.Email );
           
            s = cmd.CommandText;
            cmd.ExecuteNonQuery();
        }
       

        public ControlSolicitud getSolicitud(ControlSolicitud con)
        {
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "Select * From controlsolicitud where placa=?placa";
            cmd.Parameters.AddWithValue("?placa", con.taxi.Placa);
            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                con.respuesta = reader["respuesta"].ToString();
                con.pasajero.Email = reader["email"].ToString();
            }

            return con;
        }


        public void BorrarSolicitud(ControlSolicitud con)
        {
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "delete from controlsolicitud where (placa=?placa and email=?email)";
            cmd.Parameters.AddWithValue("?placa", con.taxi.Placa);
            cmd.Parameters.AddWithValue("?email", con.pasajero.Email);

            s = cmd.CommandText;
            cmd.ExecuteNonQuery();
        }


        public void Insertar(ControlSolicitud con)
        {
            throw new NotImplementedException();
        }


        public void Actualizar(ControlSolicitud con)
        {
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "UPDATE controlsolicitud SET respuesta=?res  WHERE (email=?email and placa=?placa)";
            cmd.Parameters.AddWithValue("?res", con.respuesta);
            cmd.Parameters.AddWithValue("?placa", con.taxi.Placa);
            cmd.Parameters.AddWithValue("?email", con.pasajero.Email);
            cmd.ExecuteNonQuery();
        }
    }
}
